/**
 * https://leetcode.cn/problems/1-bit-and-2-bit-characters/
 */
function isOneBitCharacter(bits: number[]): boolean {
  let length = bits.length;

  let i = length - 2;

  while (i >= 0 && bits[i]) {
    i--;
  }

  return ((length - i) & 1) === 0;
}

function isOneBitCharacterV1(bits: number[]): boolean {
  let index = 0;
  let single = true;
  const length = bits.length;

  while (index < length) {
    if (bits[index] === 0) {
      index++;
      single = true;
    } else if (bits[index] === 1) {
      // 后面可能是 0，也可能是 1
      const next = bits[index + 1];
      if (next === 0 || next === 1) {
        index += 2;
        single = false;
      }
    }
  }

  return single;
}
